package com.wtgroup.demo.leetcode.org_bytedance;

/**
 *
 *
 * 151. 翻转字符串里的单词
 *
 * @author dafei
 * @version 0.1
 * @date 2021/4/28 19:13
 */
public class Q_翻转字符串里的单词 {
    public static void main(String[] args) {
        String input = "the sky is blue";
        Q_翻转字符串里的单词 exe = new Q_翻转字符串里的单词();
        String res = exe.reverseWords(input);
        System.out.println(res);
    }


    public String reverseWords(String s) {
        String[] split = s.split("\\s+");
        StringBuilder sb = new StringBuilder();
        for (int i = split.length-1; i >= 0; i--) {
            String word = split[i];
            if (word.length()>0) {
                if (sb.length() > 0) {
                    sb.append(" ");
                }
                sb.append(word);
            }
        }

        return sb.toString();
    }

    public String reverseWords_2(String s) {
        char[] chars = s.toCharArray();
        int i = 0, j = 0;
        while (i<j) {
            char c = chars[i];
            chars[i] = chars[j];
            chars[j] = c;
        }

        // 单词内部反转
        return null;
    }
}
